/*****************************************************************************
* Keppe Messenger (KMP) Keppe Project-Team User Messaging System
* Copyright (C) 2013-2014 Keppe Project-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#include "frmLogin.h"
#include "frmChangePassword.h"
#include "ui_frmChangePassword.h"
#include <QDesktopWidget>
#include <QMessageBox>
QObject *formCP;

frmChangePassword::frmChangePassword(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frmChangePassword)
{
    setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
    ui->setupUi(this);
}

frmChangePassword::~frmChangePassword()
{
    delete ui;
}

void frmChangePassword::setForm(frmLogin* login)
{
    formCP = login;
}

void frmChangePassword::on_cmdChange_clicked()
{
    QString npassword = ui->txtNewPassword->text();
    QString opassword = ui->txtOldPassword->text();
    if (npassword != ui->txtRepeatPassword->text()) {QMessageBox::critical(this,"Keppe Messenger", "The passwords don't match!"); return;}
    if (npassword.length() >= 8)
    {
        if (npassword.trimmed() == npassword)
        {
            if (npassword.contains("\n") || npassword.contains("\r"))
            {
                QMessageBox::critical(this,"Keppe Messenger", "The new password contains CR or LF in binary form!");
                return;
            }
        }
        else
        {
            QMessageBox::critical(this,"Keppe Messenger", "The new password is not the same in trimmed and not trimmed form!");
            return;
        }
        if (QString::fromUtf8(npassword.toUtf8()) != QString::fromLatin1(npassword.toUtf8()))
        {
            QMessageBox::critical(this,"Keppe Messenger", "The new password is not \"UTF-8 without BOM\" compatible!");
            return;
        }
        if (QString::fromUtf8(npassword.toUtf8()) != QString::fromUtf8(npassword.toLatin1()))
        {
            QMessageBox::critical(this,"Keppe Messenger", "The new password is not \"UTF-8 without BOM\" compatible!");
            return;
        }
    }
    else
    {
        QMessageBox::critical(this,"Keppe Messenger", "The new password is shorter than 8 bytes!");
        return;
    }
    frmLogin *login = (frmLogin*)formCP;
    int ret = login->setUserPassword(opassword.trimmed(),npassword.trimmed());
    if (ret == 200)
    {
        this->close();
    }
    else if (ret == 300)
    {
        QMessageBox::critical(this,"Keppe Messenger", "The old password is not correct!");
        return;
    }
    else
    {
        QMessageBox::critical(this,"Keppe Messenger", "The server return error code " + QString::number(ret) + "!");
        return;
    }
}

void frmChangePassword::on_cmdAbort_clicked()
{
    this->close();
}

void frmChangePassword::on_cmdNumpadError_clicked()
{
    ui->txtOldPassword->setEchoMode(QLineEdit::Normal);
    ui->txtNewPassword->setEchoMode(QLineEdit::Normal);
    ui->txtRepeatPassword->setEchoMode(QLineEdit::Normal);
    ui->cmdNumpadError->setVisible(false);
}
